#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;
const int MOD = 1e9 + 7;

void matmul(ll a[4][4], ll b[4][4], ll c[4][4]) {
  ll res[4][4] = {0};
  rep(i, 0, 3) rep(j, 0, 3) rep(k, 0, 3) {
    res[i][j] = (res[i][j] + a[i][k] * b[k][j] % MOD) % MOD;
  }
  rep(i, 0, 3) rep(j, 0, 3) c[i][j] = res[i][j];
}
void matpow(ll b[4][4], ll p, ll c[4][4]) {
  ll res[4][4] = {0};
  rep(i, 0, 3) res[i][i] = 1;
  while (p) {
    if (p & 1) matmul(res, b, res);
    matmul(b, b, b);
    p >>= 1;
  }
  rep(i, 0, 3) rep(j, 0, 3) c[i][j] = res[i][j];
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    ll n, a1, a2, x, y;
    cin >> n >> a1 >> a2 >> x >> y;
    ll a3 = (a2 * x % MOD + a1 * y % MOD) % MOD;
    if (n == 1) {
      cout << a1 * a1 % MOD << endl;
      continue;
    }
    if (n == 2) {
      cout << (a1 * a1 % MOD + a2 * a2 % MOD) % MOD << endl;
      continue;
    }
    ll v[4] = {a2 * a3 % MOD, a2 * a2 % MOD, a3 * a3 % MOD,
               (a1 * a1 % MOD + a2 * a2 % MOD) % MOD};
    ll b[4][4] = {
        {y, 0, x, 0},
        {0, 0, 1, 0},
        {2 * x * y % MOD, y * y % MOD, x * x % MOD, 0},
        {0, 0, 1, 1},
    };
    matpow(b, n - 2, b);
    ll ans = 0;
    rep(i, 0, 3) ans = (ans + b[3][i] * v[i] % MOD) % MOD;
    cout << ans << endl;
  }
  return 0;
}